;; -*- mode: scheme; coding: utf-8 -*-

;;;;
;;;; Copyright (C) 2015
;;;; Christopher Allan Webber <cwebber@dustycloud.org>

;;;; This file is part of Guile-Squee.

;;;; Guile-Squee is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as
;;;; published by the Free Software Foundation, either version 3 of the
;;;; License, or (at your option) any later version.

;;;; Guile-Squee is distributed in the hope that it will be useful
;;;; WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
;;;; Public License for more details.

;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with Guile-Squee.  If not, see
;;;; <http://www.gnu.org/licenses/>.
;;;;

;;; Commentary:

;; Enums from libpq-fe.h

;;; Code:


(define-module (squee enum)
  #:use-module (rnrs enums)

  #:export (conn-status-enum
	    conn-status-enum-index
            polling-status-enum
	    polling-status-index
            exec-status-enum
	    exec-status-enum-index
            transaction-status-enum
	    transaction-status-enum-index
            verbosity-enum
	    verbosity-enum-index
            ping-enum
	    ping-enum-index

            ;; **repl and error messages only!**
            enum-set-ref))


(define conn-status-enum
  (make-enumeration
   '(connection-ok
     connection-bad
     connection-started connection-made
     connection-awaiting-response connection-auth-ok
     connection-auth-ok connection-setenv
     connection-ssl-startup
     connection-needed)))

(define conn-status-enum-index
  (enum-set-indexer conn-status-enum))

(define polling-status-enum
  (make-enumeration
   '(polling-failed
     polling-reading
     polling-writing
     polling-ok
     polling-active)))

(define polling-status-enum-index
  (enum-set-indexer polling-status-enum))

(define exec-status-enum
  (make-enumeration
   '(empty-query
     command-ok tuples-ok
     copy-out copy-in
     bad-response
     nonfatal-error fatal-error
     copy-both
     single-tuple)))

(define exec-status-enum-index
  (enum-set-indexer exec-status-enum))

(define transaction-status-enum
  (make-enumeration
   '(idle active intrans inerror unknown)))

(define transaction-status-enum-index
  (enum-set-indexer transaction-status-enum))

(define verbosity-enum
  (make-enumeration
   '(terse default verbose)))

(define verbosity-enum-index
  (enum-set-indexer verbosity-enum))

(define ping-enum
  (make-enumeration
   '(ok reject no-response no-attempt)))

(define ping-enum-index
  (enum-set-indexer ping-enum))

(define (enum-set-ref enum-set k)
  "Take an ENUM-SET and get the item at position K

This is O(n) but theoretically we don't use it much.
Again, REPL only!"
  (list-ref (enum-set->list enum-set) k))
